﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace CKS.Core.Framework
{
    public class NetworkHelper
    {
        /// <summary>
        /// 异步检测IP地址是否可达
        /// </summary>
        /// <param name="ipAddress">目标IP地址或域名</param>
        /// <param name="timeout">超时时间(毫秒)，默认1000ms</param>
        /// <returns>可达返回true，否则false</returns>
        public static async Task<bool> PingIPAsync(string ipAddress, int timeout = 1000)
        {
            try
            {
                using var ping = new Ping();
                var reply = await ping.SendPingAsync(ipAddress, timeout);
                return reply.Status == IPStatus.Success;
            }
            catch (PingException)
            {
                return false;
            }
            catch (Exception)
            {
                // 处理其他异常如无效格式等
                return false;
            }
        }
    }
}
